//
//  TodayHeartHistoryCell.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/15.
//

import UIKit
import MYModule

class HistoryCell: TableViewCell {
    
    var model: MYSQLiteTableiabe?
    
    private(set) var roundingCorners: UIRectCorner?

	override func makeUI() {
		super.makeUI()
        
        self.backgroundColor = UIColor.darkBackground
        contentView.backgroundColor = UIColor.background
		contentView.addSubview(heartStackView)
		heartStackView.addArrangedSubview(currentNumLbl)
		heartStackView.addArrangedSubview(unitLbl)
		contentView.addSubview(dateLbl)
		contentView.addSubview(iconImagView)

		heartStackView.snp.makeConstraints { make in
			make.top.equalToSuperview().offset(15)
			make.leading.equalToSuperview().offset(20)
			make.bottom.equalToSuperview().offset(-15)
		}

		iconImagView.snp.makeConstraints { make in
			make.trailing.equalToSuperview().offset(-20)
			make.centerY.equalToSuperview()
		}

		dateLbl.snp.makeConstraints { make in
            make.trailing.equalTo(iconImagView.snp.leading).offset(-16)
			make.centerY.equalToSuperview()
		}
	}

    public func bind(to model: MYSQLiteTableiabe?, currentNumColor: UIColor, until: String = "", roundingCorners: UIRectCorner? = nil) {
		
        self.model = model
        self.roundingCorners = roundingCorners
        
		currentNumLbl.textColor = currentNumColor
		let format = DateFormatter()
		format.calendar = Calendar.init(identifier: .gregorian)
		format.dateFormat = getHistoryTimeFormat()
		unitLbl.text = until
        
        self.layoutIfNeeded()
        
        let formmater = DateFormatter.localDateFormatter(dateFormat: "hh:mm aaa")
       
        if let stressModel = model as? SingleStressModel {
            currentNumLbl.text = "\(stressModel.stress)"
            let date = Date(timeIntervalSince1970: TimeInterval(stressModel.end))
            dateLbl.text = formmater.string(from: date)
            return
        }
        
        if let heartRateModel = model as? SingleHeartRateModel {
            currentNumLbl.text = "\(heartRateModel.heartRate)"
            let date = Date(timeIntervalSince1970: TimeInterval(heartRateModel.end))
            dateLbl.text = formmater.string(from: date)
            return
        }
        
        if let bloodOxygenModel = model as? SingleBloodOxygenModel {
            currentNumLbl.text = "\(bloodOxygenModel.o2)"
            let date = Date(timeIntervalSince1970: TimeInterval(bloodOxygenModel.end))
            dateLbl.text = formmater.string(from: date)
            return
        }
        
        if let hrvModel = model as? SingleHRVModel {
            currentNumLbl.text = "\(hrvModel.hrv)"
            let date = Date(timeIntervalSince1970: TimeInterval(hrvModel.end))
            dateLbl.text = formmater.string(from: date)
            return
        }
        
	}
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let roundingCorners = self.roundingCorners else {
            self.contentView.layer.mask = nil
            return
        }
        
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: roundingCorners, cornerRadii: CGSize(width: 18.0, height: 18.0))
        let maskLayer = CAShapeLayer()
        maskLayer.path = path.cgPath
        self.contentView.layer.mask = maskLayer
        
    }

	private lazy var heartStackView: StackView = {
		let heartStackView = StackView()
		heartStackView.alignment = .firstBaseline
		heartStackView.spacing = 4
		heartStackView.axis = .horizontal
		heartStackView.distribution = .fill
		return heartStackView
	}()

	public lazy var currentNumLbl: Label = {
		let currentNumLbl = Label()
		currentNumLbl.textAlignment = .right
        currentNumLbl.font = Fonts.boldSystemFont(ofSize: 26)
		return currentNumLbl
	}()

	public lazy var unitLbl: Label = {
		let untilLbl = Label()
		untilLbl.textAlignment = .right
        untilLbl.font = Fonts.systemFont(ofSize: 12)
		untilLbl.textColor = UIColor.assist_222222
		return untilLbl
	}()

	public lazy var dateLbl: Label = {
		let dateLbl = Label()
		dateLbl.font = Fonts.systemFont(ofSize: 10)
		dateLbl.numberOfLines = 0
		dateLbl.textColor = UIColor.assist_D0D0D0
		return dateLbl
	}()

	public lazy var iconImagView: ImageView = {
		let iconImagView = ImageView()
		iconImagView.image = UIImage(named: "ic_next_continue")
		return iconImagView
	}()

	override func setSelected(_ selected: Bool, animated: Bool) {
		super.setSelected(selected, animated: animated)

		// Configure the view for the selected state
	}

}
